<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Post;

class PostController extends Controller
{
    public function index(Request $request)
    {
        $map = [];
        $request->name && $map['name'] = $request->name;

        return Post::where($map)->orderBy('id')->paginate(10);
    }

    public function update(Request $request, $id)
    {
        $post = Post::find($id);
        return response()->json($post->update($request->all()));
    }

    public function show($id)
    {
        return Post::find($id);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        return  Post::create($data);
    }

    public function all()
    {
        return Post::all();
    }

//    public function destroy($id)
//    {
//        $post = Post::find($id);
//        $post && $post->delete();
//        return $post;
//    }
}
